{% extends 'df_goods/base_detail_list.html' %}
{% load staticfiles %}
{% block title %}天天生鲜-商品列表{% endblock title %}
{% block main_content %}
    <div class="breadcrumb">
        <a href="#">全部分类</a>
        <span>></span>
        <a href="#">{{ type.name }}</a>
    </div>

    <div class="main_wrap clearfix">
        <div class="l_wrap fl clearfix">
            <div class="new_goods">
                <h3>新品推荐</h3>
                <ul>
                    {% for sku in new_skus %}
                        <li>
                            <a href="{% url 'goods:detail' sku.id %}"><img src="/media/{{ sku.image }}"></a>
                            <h4><a href="{% url 'goods:detail' sku.id %}">{{ sku.name }}</a></h4>
                            <div class="prize">￥{{ sku.price }}</div>
                        </li>
                    {% endfor %}
                </ul>
            </div>
        </div>

        <div class="r_wrap fr clearfix">
            <div class="sort_bar">
                <a href="{% url 'goods:list' type.id 1 %}" {% if sort == 'default' %}class="active"{% endif %}>默认</a>
                <a href="{% url 'goods:list' type.id 1 %}?sort=price" {% if sort == 'price' %}class="active"{% endif %}>价格</a>
                <a href="{% url 'goods:list' type.id 1 %}?sort=hot"
                   {% if sort == 'hot' %}class="active"{% endif %}>人气</a>
            </div>

            <ul class="goods_type_list clearfix">
                {# {% for sku in skus_page.object_list %}  如下一样的#}
                {% for sku in skus_page %}
                    {% csrf_token %}
                    <li>
                        <a href="{% url 'goods:detail' sku.id %}"><img src="{{ sku.image.url }}"></a>
                        <h4><a href="{% url 'goods:detail' sku.id %}">{{ sku.name }}</a></h4>
                        <div class="operate">
                            <span class="prize">￥{{ sku.price }}</span>
                            <span class="unit">{{ sku.price }}/{{ sku.unite }}</span>
                            <a href="javascript:;" class="add_goods add_cart" sku_id="{{ sku.id }}"></a>
                        </div>
                    </li>
                {% endfor %}
            </ul>

            <div class="pagenation">
                {# 有上一页 #}
                {% if skus_page.has_previous %}
                    <a href="{% url 'goods:list' type.id skus_page.previous_page_number %}?sort={{ sort }}">上一页</a>
                {% endif %}

                {% for pageindex in skus_page.paginator.page_range %}
                    {% if pageindex == skus_page.number %}
                        <a href="{% url 'goods:list' type.id pageindex %}?sort={{ sort }}"
                           class="active">{{ pageindex }}</a>
                    {% else %}
                        <a href="{% url 'goods:list' type.id pageindex %}?sort={{ sort }}">{{ pageindex }}</a>
                    {% endif %}
                {% endfor %}

                {# 有下一页 #}
                {% if skus_page.has_next %}
                    <a href="{% url 'goods:list' type.id skus_page.next_page_number %}?sort={{ sort }}">下一页</a>
                {% endif %}

            </div>
        </div>
    </div>
{% endblock main_content %}
{% block bottom %}
    <div class="add_jump"></div>
{% endblock bottom %}
{% block bottomfiles %}
    <script type="text/javascript" src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
    <script type="text/javascript">
        $('.add_cart').click(function () {
            // 获取add_cart a标签左上角的坐标
            var $add_x = $(this).offset().top;
            var $add_y = $(this).offset().left;
            // 获取show_count div元素左上角的坐标
            var $to_x = $('#show_count').offset().top;
            var $to_y = $('#show_count').offset().left;
            // 获取商品id和商品数目
            count = 1;
            sku_id = $(this).attr('sku_id');// prop attr
            csrf = $('input[name="csrfmiddlewaretoken"]').val();
            // 组织参数
            params = {'sku_id': sku_id, 'count': count, 'csrfmiddlewaretoken': csrf};
            // 发起ajax post请求，访问/cart/add, 传递参数:sku_id count
            $.post('{% url 'cart:add' %}', params, function (data) {
                if (data.res == 5) {
                    // 添加成功
                    $(".add_jump").css({'left': $add_y, 'top': $add_x, 'display': 'block'});
                    $(".add_jump").stop().animate({
                            'left': $to_y + 7,
                            'top': $to_x + 7
                        },
                        "fast", function () {
                            $(".add_jump").fadeOut('fast', function () {
                                // 重新设置用户购物车中商品的条目数
                                $('#show_count').html(data.cart_count);
                            });
                        });
                } else {
                    alert(data.errmsg)
                }
            })

        })
    </script>
{% endblock %}